
package com.contractmodule.dao;
import com.contractmodule.bean.*;
import java.sql.*;
import java.io.*;
import java.util.*;
import util.*;
public class TbDispatchListDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbDispatchList tbDispatchList=null;
    private List<TbDispatchList> tbDispatchListList=null;

    public boolean add(TbDispatchList inTbDispatchList){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_DispatchList(DispatchListCode,ContractCode,CompanyProductTypeMiddleId,DispatchListName,DispatchListBudget,UserId) values(?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbDispatchList.getDispatchListCode());
        pt.setString(2,inTbDispatchList.getContractCode());
        pt.setInt(3,inTbDispatchList.getCompanyProductTypeMiddleId());
        pt.setString(4,inTbDispatchList.getDispatchListName());
        pt.setDouble(5,new Double(inTbDispatchList.getDispatchListBudget()));
        pt.setInt(6,inTbDispatchList.getUserId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public boolean del(int inDispatchListId){
        return UtilDAO.del("Tb_DispatchList", "DispatchListId", inDispatchListId);
    }
    public boolean edit(TbDispatchList inTbDispatchList){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_DispatchList set ContractCode=?,CompanyProductTypeMiddleId=?,DispatchListName=?,DispatchListBudget=?,UserId=?,CreateTime=getdate() where DispatchListCode=?";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbDispatchList.getContractCode());
        pt.setInt(2,inTbDispatchList.getCompanyProductTypeMiddleId());
        pt.setString(3,inTbDispatchList.getDispatchListName());
        pt.setDouble(4,new Double(inTbDispatchList.getDispatchListBudget()));
        pt.setInt(5,inTbDispatchList.getUserId());
        pt.setString(6,inTbDispatchList.getDispatchListCode());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public TbDispatchList getTbDispatchListByDispatchListId(int inDispatchListId) {
        tbDispatchList=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DispatchList where DispatchListId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inDispatchListId);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbDispatchList = getTbDispatchList(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDispatchList;
    }
    public TbDispatchList getTbDispatchListByDispatchListCode(String inDispatchListCode) {
        tbDispatchList=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DispatchList where DispatchListCode=? ";
            pt = conn.prepareStatement(sql);
            pt.setString(1,inDispatchListCode);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbDispatchList = getTbDispatchList(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDispatchList;
    }
    public List<TbDispatchList> getTbDispatchListByContractCode(String contractCode) {
    	tbDispatchListList=new ArrayList<TbDispatchList>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DispatchList where ContractCode=? ";
            pt = conn.prepareStatement(sql);
            pt.setString(1,contractCode);
            
            rs = pt.executeQuery();
            while (rs.next()) {
            	  tbDispatchListList.add(getTbDispatchList(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDispatchListList;
    }
    public List<TbDispatchList> getTbDispatchListAll() {
        tbDispatchListList=new ArrayList<TbDispatchList>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DispatchList";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbDispatchListList.add(getTbDispatchList(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDispatchListList;
    }
    public List<TbDispatchList> getTbDispatchListAll(VskPage vp) {
        tbDispatchListList=new ArrayList<TbDispatchList>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DispatchList";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_DispatchList", "DispatchListId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbDispatchListList.add(getTbDispatchList(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDispatchListList;
    }
    public TbDispatchList getTbDispatchList(ResultSet rs) throws Exception{
        TbDispatchList tbDispatchList = new TbDispatchList();
        tbDispatchList.setDispatchListId(rs.getInt("DispatchListId"));
        tbDispatchList.setDispatchListCode(rs.getString("DispatchListCode"));
        tbDispatchList.setContractCode(rs.getString("ContractCode"));
        tbDispatchList.setCompanyProductTypeMiddleId(rs.getInt("CompanyProductTypeMiddleId"));
        tbDispatchList.setDispatchListName(rs.getString("DispatchListName"));
        tbDispatchList.setDispatchListBudget(rs.getString("DispatchListBudget"));
        tbDispatchList.setUserId(rs.getInt("UserId"));
        tbDispatchList.setCreateTime(rs.getString("CreateTime"));
        return tbDispatchList;
    }
}
